/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* -------
* Table 6
* -------

* Globals
global dep "tax_rate risk ln_iwei nup_gap_i_int htrl_i_int"
global dep_plus_eis "eis_f tax_rate risk ln_iwei nup_gap_i_int htrl_i_int"

* Matrix
mat table6_1 = J(4,6,.)
mat table6_1T = J(1,6,.)
forvalues x = 2(1)3 {
	mat table6_`x' = J(4,5,.)
	mat table6_`x'T = J(1,5,.)
}
mat table6_4 = J(8,5,.)
mat table6_4T = J(1,5,.)

* Loop for estimations
** Panel A
local i = 1
local j = 1
foreach x in $dep_plus_eis {

	qui: reghdfe `x' wfr if sample_int==1 & wfr!=. , vce(cluster insp_f key_dec) noabsorb

	mat table6_1[`i',`j'] = _b[wfr]
	local i = `i' + 1
	mat table6_1[`i',`j'] = _se[wfr]
	mat table6_1T[1,`j'] = e(N)
	local i = `i' + 2
	mat table6_1[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}
** Panel B
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_nowfr_f if sample_int==1 & wfr==0 , vce(cluster insp_f key_dec) noabsorb

	mat table6_2[`i',`j'] = _b[eis_nowfr_f]
	local i = `i' + 1
	mat table6_2[`i',`j'] = _se[eis_nowfr_f]
	mat table6_2T[1,`j'] = e(N)
	local i = `i' + 2
	mat table6_2[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}
** Panel C
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f if sample_int==1 & wfr!=. , vce(cluster insp_f key_dec) noabsorb

	mat table6_3[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table6_3[`i',`j'] = _se[eis_f]
	mat table6_3T[1,`j'] = e(N)
	local i = `i' + 2
	mat table6_3[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}
** Panel D
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' wfr eis_f wfr_eis_f if sample_int==1 & wfr!=. , vce(cluster insp_f key_dec) noabsorb

	mat table6_4[`i',`j'] = _b[wfr]
	local i = `i' + 1
	mat table6_4[`i',`j'] = _se[wfr]
	local i = `i' + 1
	mat table6_4[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table6_4[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat table6_4[`i',`j'] = _b[wfr_eis_f]
	local i = `i' + 1
	mat table6_4[`i',`j'] = _se[wfr_eis_f]
	mat table6_4T[1,`j'] = e(N)
	local i = `i' + 2
	mat table6_4[`i',`j'] = e(r2)
	local i = `i' -7
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table 6.xlsx", replace sheet(Table6) // create a new excel spreadsheet

* Title
putexcel (A1:G1), merge hcenter vcenter
putexcel A1 = "During delegated randomization of inspector assignment"

* Dependent variables
putexcel A2 = "Dependent variable:", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "G" "'
local labels `" "Excess interaction share" "Tax rate" "Risk score" "Log initial weight" "Log initial unit price (relative to internal prices)" "Initial hyp. tax revenue losses" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel `y'2 = "`x'", hcenter vcenter bold
}

* Panels
** Name
local cells `" "4" "11" "18" "25""'
local labels `" "A. Withheld from randomization" "B. Excess interaction - delegated randomized declarations only" "C. Excess interaction" "D. Combined measures" "'
forvalues i = 1(1)4 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel (A`y':G`y'), merge hcenter vcenter bold
	putexcel A`y' = "`x'"
}
** Idependent variables
local cells `" "6" "27" "13" "20" "29" "31" "'
local labels `" "Withheld from randomization (WFR)" "Withheld from randomization (WFR)" "Random excess interaction share" "Excess interaction share" "Excess interaction share" "WFR*Excess interaction share" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel A`y' = "`x'", left vcenter
}
** Coefficients
local i = 1
forvalues x = 6(7)27 {
	
	if "`x'" == "6" {
		putexcel B`x' = matrix(table6_`i'), nformat(0.000) hcenter vcenter
	}
	
	else {
		putexcel C`x' = matrix(table6_`i'), nformat(0.000) hcenter vcenter
	}
	
	local i = `i' +1
}
** Observations
local i = 1
foreach x in 8 15 22 33 {
	putexcel A`x' = "Observations", left vcenter
	
	if "`x'" == "8" {
		putexcel B`x' = matrix(table6_`i'T), nformat(number_sep) hcenter vcenter
	}
	
	else {
		putexcel C`x' = matrix(table6_`i'T), nformat(number_sep) hcenter vcenter
	}
	
	local i = `i' +1
}
** R2
foreach x in 9 16 23 34 {
	putexcel A`x' = "R-squared", left vcenter
}
** Column numbers
global cells "B C D E F G C D E F G C D E F G C D E F G"
forvalues i = 1(1)21 {
	
	local y: word `i' of $cells

	if `i' < 7 {
		putexcel `y'5 = "(`i')", hcenter vcenter
	}
	
	if `i' > 6 & `i' < 12 {
		putexcel `y'12 = "(`i')", hcenter vcenter
	}
	
	if `i' > 11 & `i' < 17 {
		putexcel `y'19 = "(`i')", hcenter vcenter
	}
	
	if `i' > 16 {
		putexcel `y'26 = "(`i')", hcenter vcenter
	}
}
macro drop cells


* -------------------------------- ENDS HERE -------------------------------- *